package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.CommonAdapter;
import sdk.webview.fmc.com.fmcsdk.base.ViewHolder;
import sdk.webview.fmc.com.fmcsdk.bean.DDTypeBean;

public class LangDialog extends Dialog {

    private final List<DDTypeBean.RecordsBean> list;
    private final Context context;

    public LangDialog(@NonNull Context context, List<DDTypeBean.RecordsBean> list) {
        super(context);
        this.context = context;
        this.list = list;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lang_dialog);
        //试着提示框的位置和大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
       ListView listView = (ListView) findViewById(R.id.listview);
       LangAdapter langAdapter = new LangAdapter(context,list,R.layout.item_lang);
       listView.setAdapter(langAdapter);
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (clicklistener!=null){
                    clicklistener.onItemClick(i);
                }
           }
       });
    }

    class LangAdapter extends CommonAdapter<DDTypeBean.RecordsBean>{

        public LangAdapter(Context context, List<DDTypeBean.RecordsBean> mDatas, int itemLayoutId) {
            super(context, mDatas, itemLayoutId);
        }

        @Override
        public void convert(ViewHolder helper, DDTypeBean.RecordsBean item, int position) {
            if (item.getCode().equalsIgnoreCase("zh-CN")){
                helper.setText(R.id.text1,"简体中文");
            }else if (item.getCode().equalsIgnoreCase("EN-US")){
                helper.setText(R.id.text1,"English");
            }else if (item.getCode().equalsIgnoreCase("zh-CHT")){
                helper.setText(R.id.text1,"繁體中文");
            }

        }
    }


    public void setOnItemClicklistener( ClickListenerInterface clicklistener) {
        this.clicklistener = clicklistener;
    }
    public ClickListenerInterface clicklistener;
    public interface ClickListenerInterface {

        void onItemClick(int postion);


    }
}
